#-*- coding: utf-8 -*-

#imports needed
from GetData import GetData
from GetStatistics import GetStatistics
import matplotlib.pyplot as plt
import numpy as np

#class
class getGrafs:
    #Constructor
    def __init__(self):
        self.stat = GetStatistics()
        pass

    #get graf of teachers by year off all years
    def teachers_year(self):
        print "Building graph! Please what..."
        self.stat.cursor = self.stat.connect.cursor()
        list = []
        years = []
        for i in range(0, 10):
            count = self.stat.get_num_teachers_year(i)
            list.append(count)
            years.append(2000 + i)
        self.stat.cursor.close()
        self.histogram_base('Number of Teachers by Year','Years','Teachers',years, list)

    #get graf for anothers estatistics
    #@param whatToSearch - what columm you want to search
    #@param card - what card you want to search
    #@param year - year to search
    def teachers_anyting_year(self, whatToSearch, card, year):
        print "Building graph! Please what..."
        self.stat.cursor = self.stat.connect.cursor()
        if card != "degree":
            list = self.stat.get_num_teachers_anywhere_per_year(whatToSearch, card, year)
        else:
            list = self.stat.get_num_teachers_degree_establishment_year(year)
        institutions = []
        numTeachersList = []
        for institution, numTeachers in list.iteritems():
            institutions.append(institution)
            numTeachersList.append(numTeachers)
        self.stat.cursor.close()
        self.histogram_base("Number of Teachers by {0} on year of 200{1}".format(whatToSearch, year),whatToSearch,'Number of Teachers',institutions, numTeachersList)

    #do all the graf, if as more than 15 bars he dont do the names of the bars
    #@param title - title of the graf
    #@param xLabel - legend of xx
    #@param yLabel - legend of yy
    #@param x - x values
    #@param y - y values
    def histogram_base(self, title, xLabel, yLabel, x, y):
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)
        N = len(y)
        ind = range(N)
        ax.bar(ind, y, facecolor='#777777', align='center', ecolor='black')
        ax.set_ylabel(yLabel)
        ax.set_xlabel(xLabel)
        ax.set_title(title,fontstyle='italic')
        if len(x) < 15:
            ax.set_xticks(ind)
            group_labels = x
            ax.set_xticklabels(group_labels)
        fig.autofmt_xdate()
        plt.axis([-1, len(x)+ 1, min(y) - min(y)/10, max(y) + max(y)/100])
        print "DONE!"
        plt.show()
